package com.huifei.controller;

import com.huifei.parseexcel.SubExcelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;

/**
 * @author: huifei
 * @date: 2017-09-08 13:39
 */
@Controller
public class ExportController {

    @Autowired
    private SubExcelService subExcelService;

    @RequestMapping("/")
    public String index(ModelMap map) {
        map.addAttribute("test", "欢迎进入人事数据导出系统");
        return "index";
    }

    @RequestMapping(value = "/export", method = {RequestMethod.POST})
    @ResponseBody
    public String export(@RequestParam Map<String, String> params) {

        StringBuilder sb = new StringBuilder();

        String sumExcel = params.get("sumExcel");
        if (StringUtils.isBlank(sumExcel)) {
            sb.append("人事excel不能为空--");
        }
        File file1 = new File(sumExcel);
        if (!file1.exists()) {
            sb.append("人事exce1路径不存在--");
        }

        String chendouExcel = params.get("chendouExcel");
        File file2 = new File(chendouExcel);
        if (!file2.exists()) {
            sb.append("成都exce1路径不存在--");
        }
        String shanghaiExcel = params.get("shanghaiExcel");
        File file3 = new File(shanghaiExcel);
        if (!file3.exists()) {
            sb.append("上海exce1路径不存在--");
        }
        try {
            if (StringUtils.isNotBlank(chendouExcel)) {
                subExcelService.parseExcel(chendouExcel, sumExcel);
            }

            if (StringUtils.isNotBlank(shanghaiExcel)) {
                subExcelService.parseExcel(shanghaiExcel, sumExcel);
            }

        } catch (Exception e) {
            sb.append("请关闭打开的成都或者上海团队excel");
            e.printStackTrace();
        }
        return sb.toString();
    }
}
